<?php
namespace Home\Controller;
use Think\Controller;
use Common\Model\BonusTypeModel;
use Common\Model\BonusLogModel;
use Common\Model\FinanceLogModel;
use Common\Model\FinanceTypeModel;
use Common\Model\UserMoneyModel;
use Common\Model\CurrencyModel;
use Common\Model\ChangeSysModel;
use Common\Model\UserModel;
use Common\Model\ChangeLogModel;
use Common\Model\UserBankModel;
use Common\Model\SellModel;
use Common\Model\SendLogModel;
use Common\Api\Reward;
use Common\Model\ConfigModel;
use Common\Model\WithdrawalsModel;
class MoneyController extends HomeController {
    public function __construct()
    {
        parent::__construct();
    }
    public function bonusLog()
    {
        $bonus_type=BonusTypeModel::newSelf()->getInfo();
        
        $where['user_id']=$_SESSION['USER_KEY_ID'];
        $list=BonusLogModel::newSelf()->pagingList($where);
        
        $this->assign('bonus_type',$bonus_type);
        $this->assign('list',$list);
        
        $this->display();
    }
    public function financeLog()
    {
        $finance_log=FinanceTypeModel::newSelf()->getInfo();
        
        $where['user_id']=$_SESSION['USER_KEY_ID'];
        $list=FinanceLogModel::newSelf()->pagingList($where);
        
        $this->assign('finance_log',$finance_log);
        $this->assign('list',$list);
        
        $this->display();
        
    }
    public function changeMoney()
    {
        $info=UserMoneyModel::newSelf()->getUserMoneyInfo($_SESSION['USER_KEY_ID']);
        $this->assign('info',$info);
        
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
        
        $need_currency=ChangeSysModel::newSelf()->getNeedCurrencys();
        $this->assign('need_currency',$need_currency);
        
        $change_sys=ChangeSysModel::newSelf()->getInfo();
        $this->assign('change_sys',$change_sys);
        $this->display();
        
    }
    public function changeMoneySub()
    {
        $data['sys_id']=(int)I('sys_id');
        $data['num']=I('num');
        $data['twopassword']=I('twopassword');
        
        $this->notEmptyValidate($data['sys_id'],'请选择转换类型');
        $this->notEmptyValidate($data['num'],'请输入转换金额');
        $this->notEmptyValidate($data['twopassword'],'请输入二级密码');
        $this->numVerification($data['num'],'请输入正确金额');
        $this->gtValidate($data['num'],0,'请输入正确金额');
        
        $res=UserModel::newSelf()->checkUserTwoPassword($data['twopassword'], $this->home_user_info['twopassword']);
        $this->trueValidate($res,'二级密码不正确');
        
        unset($data['twopassword']);
        
        $sys=ChangeSysModel::newSelf()->getInfo();
        $this->notEmptyValidate($sys[$data['sys_id']], '请正确选择转帐类型');
        
        $currency_id=$sys[$data['sys_id']]['currency_id'];
        $to_currency_id=$sys[$data['sys_id']]['to_currency_id'];

        $currency=CurrencyModel::newSelf()->getInfo();
        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->egtValidate($user_money[$currency_id]['num'],$data['num'],'您的'.$currency[$currency_id]['name'].'不足');
        
        
        $model=ChangeLogModel::newSelf();
        
        $data['add_time']=time();
        $data['status']='0';
        $data['remark']=I('remark');
        $data['user_id']=$this->home_user_info['id'];
        
        UserMoneyModel::newSelf()->decUserMoney($this->home_user_info['id'],$currency_id,$data['num']);
        UserMoneyModel::newSelf()->incUserMoney($this->home_user_info['id'],$to_currency_id,$data['num']*(1-$this->config['CHANGE_FEE']));
        
        $reward=Reward::newSelf($this->home_user_info, $data['num']);
        $reward->updateBonusLog($this->home_user_info['id'],['bonus7'=>$data['num']*$this->config['CHANGE_FEE']]);
        $res=$model->add($data);
        
        $this->resReturn($res, '转换成功及时生效', '系统繁忙请稍后再试');
    }
    public function sellMoney()
    {
        $this->return_type='error';
        
        $id=I('id');
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->notEmptyValidate($currency[$id],'请正确操作');
        $this->eqValidate($currency[$id]['sell'],'1','该币种暂时未开通交易');
        $this->assign('currency',$currency[$id]);

        $info=UserMoneyModel::newSelf()->getUserMoneyInfo($_SESSION['USER_KEY_ID']);
        $this->assign('info',$info);
        
        $where['user_id']=$this->home_user_info['id'];
        $user_bank=UserBankModel::newSelf()->pagingList($where);
        
        $this->assign('user_bank',$user_bank);
        $this->display();
    }
    public function sellMoneySub()
    {
        
        $data['currency_id']=I('id');
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->notEmptyValidate($currency[$data['currency_id']],'请正确操作');
        $currency=$this->eqValidate($currency[$data['currency_id']]['sell'],'1','该币种暂时未开通交易');
        $data['num']=I('num');
        
        $this->notEmptyValidate($data['num'],'请输入挂单金额');
        $this->gtValidate($data['num'],0,'请输入正确金额');
        
        $data['twopassword']=I('twopassword');
        $this->notEmptyValidate($data['twopassword'],'请输入二级密码');
        $this->numVerification($data['num'],'请输入正确金额');

        $res=UserModel::newSelf()->checkUserTwoPassword($data['twopassword'], $this->home_user_info['twopassword']);
        
        $this->trueValidate($res,'二级密码不正确');
        
        unset($data['twopassword']);
        

        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->egtValidate($user_money[$data['currency_id']]['num'],$data['num'],'您的'.$currency[$data['currency_id']]['name'].'不足');
        
        $data['bank_id']=I('bank_id');
        $user_bank=UserBankModel::newSelf()->findOneById($data['bank_id']);
        $this->notEmptyValidate($user_bank,'收款方式有误');
        $this->eqValidate($user_bank['user_id'], $this->home_user_info['id'],'收款方式有误');

        $data['add_time']=time();
        $data['status']=0;
        $data['sell_id']=$this->home_user_info['id'];
        UserMoneyModel::newSelf()->decUserMoney( $this->home_user_info['id'], $data['currency_id'], $data['num']);
        
        
        $res=SellModel::newSelf()->add($data);
        $this->resReturn($res, '挂单成功', '系统繁忙请稍后再试');
    }

    public function buyMoney()
    {
        $this->return_type='error';
    
        $id=I('id');
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->notEmptyValidate($currency[$id],'请正确操作');
        $this->eqValidate($currency[$id]['sell'],'1','该币种暂时未开通交易');
        $this->assign('currency',$currency[$id]);
    
        $country=$this->getCountry();
        $this->assign('country',$country);
        
        $where['user_id']=['neq',$this->home_user_info['id']];
        $where['status']=0;
        $list=SellModel::newSelf()->pagingList($where);
    
        
        $this->assign('list',$list);
        $this->display();
    }

    public function buyMoneySub()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
        
        $this->eqValidate($sell_info['status'],'0','此挂单占时无法购买');
        $this->neqValidate($sell_info['sell_id'], $this->home_user_info['id'],'不可以购买自己的挂单');
        
        $data['buy_id']=$this->home_user_info['id'];
        $data['buy_time']=time();
        $data['status']=1;

        $res=$sell_model->where(['id'=>$id])->save($data);
        $this->resReturn($res,'购买成功','系统繁忙请稍后再试');
    }
    public function sellLog()
    {
        $where['sell_id']=$this->home_user_info['id'];
        $list=SellModel::newSelf()->pagingList($where);
        $this->assign('list',$list);
        $this->display();
    }
    public function revokeSell()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
        
        $this->eqValidate($sell_info['status'],'0','此挂单无法撤销');
        $this->eqValidate($sell_info['sell_id'], $this->home_user_info['id'],'请正确操作');
        

        $data['success_time']=time();
        $data['status']=-1;
        $res=$sell_model->where(['id'=>$id])->save($data);
        
        UserModel::newSelf()->incUserMondey($sell_info['sell_id'], $sell_info['num'], $sell_info['currency_id']);
        
        $this->resReturn($res,'撤单成功','系统繁忙请稍后再试');
    }
    public function buyLog()
    {
        $where['buy_id']=$this->home_user_info['id'];
        $list=SellModel::newSelf()->pagingList($where);
        $this->assign('bank_type',$this->getBankType());
        $this->assign('list',$list);
        $this->display();
    }
    public function giveUpBuySub()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
        
        $this->eqValidate($sell_info['status'],'1','此挂单无法放弃');
        $this->eqValidate($sell_info['buy_id'], $this->home_user_info['id'],'请正确操作');

        $data['buy_id']='';
        $data['buy_time']='';
        $data['status']='';
        
        $res=$sell_model->where(['id'=>$id])->save($data);
        $this->resReturn($res,'成功放弃此次交易','系统繁忙请稍后再试');
        
    }
    public function paySellSub()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
        
        $this->eqValidate($sell_info['status'],'1','此挂单无法付款确认');
        $this->eqValidate($sell_info['buy_id'], $this->home_user_info['id'],'请正确操作');

        $data['pay_time']=time();
        $data['status']=2;
        
        $res=$sell_model->where(['id'=>$id])->save($data);
        $this->resReturn($res,'付款确认成功','系统繁忙请稍后再试');
        
    }
    public function sureSellSub()
    {
        $id=I('id');
        $this->notEmptyValidate($id,'请正确操作');
        $sell_model=SellModel::newSelf();
        $sell_info=$sell_model->findOneById($id);
        
        $this->eqValidate($sell_info['status'],'2','此挂单无法收款');
        $this->eqValidate($sell_info['sell_id'], $this->home_user_info['id'],'请正确操作');

        $data['success_time']=time();
        $data['status']=3;
        
        $res=$sell_model->where(['id'=>$id])->save($data);
        UserModel::newSelf()->incUserMondey($sell_info['buy_id'], $sell_info['num'], $sell_info['currency_id']);
        $this->resReturn($res,'交易成功','系统繁忙请稍后再试');
        
    }
    public function sendMoney()
    {
        $this->return_type='error';
        
        $id=I('id');
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->notEmptyValidate($currency[$id],'请正确操作');
        $this->eqValidate($currency[$id]['send'],'1','该币种暂时未开通转账');
        
        $this->assign('currency',$currency[$id]);

        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($_SESSION['USER_KEY_ID']);
        $this->assign('user_money',$user_money);
        
        $where['user_id']=$this->home_user_info['id'];
        $this->display();
    }
    public function sendMoneySub()
    {
        $data['currency_id']=I('id');
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->notEmptyValidate($currency[$data['currency_id']],'请正确操作');
        $this->eqValidate($currency[$data['currency_id']]['send'],'1','该币种暂时未开通转账');

        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($_SESSION['USER_KEY_ID']);

        $rs=checkPhoneCode(I('phone_code'),'send',$this->home_user_info['phone']);
        
        $this->trueValidate($rs,'手机验证码不正确');

        $data['num']=I('num');

        $this->notEmptyValidate($data['num'],'请输入转账金额');
        $this->gtValidate($data['num'],0,'请输入正确金额');
        
        $this->numVerification($data['num'],'请输入正确金额');

        $data['username']=I('username');
        $this->notEmptyValidate($data['username'],'请输入转入用户');
        $send_user=UserModel::newSelf()->findUserByUsername($data['username']);
        $this->notEmptyValidate($send_user,'转入用户不存在');
        $data['receive_id']=$send_user['id'];

        $password=I('twopassword');
        $this->notEmptyValidate($password,'请输入二级密码');
        $res=UserModel::newSelf()->checkUserTwoPassword($password, $this->home_user_info['twopassword']);
        $this->trueValidate($res,'二级密码不正确');

        
        $currency=CurrencyModel::newSelf()->getInfo();

        $money_model=UserMoneyModel::newSelf();
        $user_money=$money_model->getUserMoneyInfo($this->home_user_info['id']);
        $this->egtValidate($user_money[$data['currency_id']]['num'],$data['num'],'您的'.$currency[$data['currency_id']]['name'].'不足');
        
        $data['remark']=I('remark');
        $data['user_id']=$this->home_user_info['id'];
        $data['add_time']=time();
        
        $money_model->incUserMoney($send_user['id'], $data['currency_id'], $data['num']);
        $money_model->decUserMoney($this->home_user_info['id'], $data['currency_id'], $data['num']);
        $res=SendLogModel::newSelf()->add($data);
        $this->resReturn($res,'转账成功','系统繁忙请稍后再试');
    }
    public function sendLog()
    {

        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
        
        $where['user_id']=$_SESSION['USER_KEY_ID'];
        $where['receive_id']=$_SESSION['USER_KEY_ID'];
        $where['_logic']='or';
        
        $list=SendLogModel::newSelf()->pagingList($where);
        $this->assign('list',$list);
        $this->display();
    }

    public function changeLog()
    {
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
    
        $change_sys=ChangeSysModel::newSelf()->getInfo();
        $this->assign('change_sys',$change_sys);
    
        $where['user_id']=$_SESSION['USER_KEY_ID'];
    
        $list=ChangeLogModel::newSelf()->pagingList($where);
        $this->assign('list',$list);
        $this->display();
    }
    /**
     *账户充值 
     */
    public function full(){
    	$this->return_type='error';
    	
    	$id=I('id');
    	$currency=CurrencyModel::newSelf()->getInfo();
    	$this->notEmptyValidate($currency[$id],'请正确操作');
    	$this->eqValidate($currency[$id]['send'],'1','该币种暂时未开通充值');
    	$this->assign('currency',$currency[$id]);

    	$config=ConfigModel::newSelf()->getInfo();
    	$this->assign('config',$config);
    	
    	$user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($_SESSION['USER_KEY_ID']);
    	$this->assign('user_money',$user_money);
    	$this->display();
    }

    /**
     *账户提现
     */
    public function cash(){
    	$this->return_type='error';
    	$id=I('id');
    	$currency=CurrencyModel::newSelf()->getInfo();
    	$this->notEmptyValidate($currency[$id],'请正确操作');
    	$this->eqValidate($currency[$id]['withdrawals'],'1','该币种暂时不能提现');
    	$this->assign('currency',$currency[$id]);
    
    	$user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($_SESSION['USER_KEY_ID']);
    	$this->assign('user_money',$user_money);
    	
        $where['user_id']=$this->home_user_info['id'];
    	$this->display();
    }

    public function withdrawalsSub()
    {
        $data['currency_id']=1;

        $this->neqValidate($this->home_user_info['activation'],1,'您的账户无法提现');
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->notEmptyValidate($currency[$data['currency_id']],'请正确操作');
        $currency=$this->eqValidate($currency[$data['currency_id']]['withdrawals'],'1','该币种暂时不能提现');
    	
    	$rs=checkPhoneCode(I('phone_code'),'cash',$this->home_user_info['phone']);
        
        $this->trueValidate($rs,'手机验证码不正确');
        
        $data['num']=I('num');
        $this->egtValidate($data['num'],$this->config['WITHDRAWALS_MIN'],'最低提现金额'.$this->config['WITHDRAWALS_MIN']);
         
        $base=$data['num']%$this->config['WITHDRAWALS_NUM_BASE'];
        $this->eqValidate($base,0,'提现金额必须为'.$this->config['WITHDRAWALS_NUM_BASE'].'整数倍');
    
        $this->notEmptyValidate($data['num'],'请输入提现金额');
        $this->gtValidate($data['num'],0,'请输入正确金额');
    
        $data['twopassword']=I('twopassword');
        $this->notEmptyValidate($data['twopassword'],'请输入二级密码');
        $this->numVerification($data['num'],'请输入正确金额');
    
        $res=UserModel::newSelf()->checkUserTwoPassword($data['twopassword'], $this->home_user_info['twopassword']);
    
        $this->trueValidate($res,'二级密码不正确');
    
        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->egtValidate($user_money[$data['currency_id']]['num'],$data['num'],'您的余额不足');
    
        unset($data['twopassword']);
    
        $data['add_time']=time();
        $data['status']=0;
        $data['remark']=I('remark');
        $data['user_id']=$this->home_user_info['id'];
        $data['fee']=$data['num']*$this->config['WITHDRAWALS_FEE'];
        UserMoneyModel::newSelf()->decUserMoney( $this->home_user_info['id'], $data['currency_id'], $data['num']);
    
    
        $res=WithdrawalsModel::newSelf()->add($data);
        $this->resReturn($res,'提现申请成功', '提现申请失败');
         
    }
    public function cashLog()
    {
        $where['user_id']=$_SESSION['USER_KEY_ID'];
        
        $list=WithdrawalsModel::newSelf()->pagingList($where);
        $this->assign('list',$list);
        $this->display();
    }
    
    public function sendCashPhoneCode(){
    	$phone=$this->home_user_info['phone'];
    	 $a=phoneCode($phone,'cash');
    	 $this->ajaxReturn($a);
    }
    
	//转账
    public function sendMoneyPhoneCode(){
    	$phone=$this->home_user_info['phone'];
    	$a=phoneCode($phone,'send');
    	$this->ajaxReturn($a);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}